import { type Category } from "../../../types/content/category";
import {
  Button,
  Pagination,
  Space,
  Table,
  TableProps,
  Image,
  Modal,
} from "antd";
import PermissionBtn from "@/components/PermissionBtn";
import QueryForm from "./components/QueryForm";
import { ExclamationCircleFilled, PlusOutlined } from "@ant-design/icons";
import { useCategoryTable } from "./hooks/useCategoryTable";
import { useCategoryOption } from "./hooks/useCategoryOption";
import OptionForm from "./components/OptionForm";

export default function Category() {
  const {
    tableData,
    pagination,
    loading,
    setPagination,
    setSearchForm,
    removeCategory,
    getCategoryList
  } = useCategoryTable();
  const {
    isModalOpen,
    optionType,
    oldInfo,
    close,
    openOptionDialog
  }= useCategoryOption()
  const showConfirm = (row: Category.Row) => {
    Modal.confirm({
      title: "警告",
      icon: <ExclamationCircleFilled />,
      content: `确认删除【${row.name}】?`,
      okText: "确认",
      cancelText: "取消",
      okType: "danger",
      onOk() {
        removeCategory(row);
      },
    });
  };
  const columns: TableProps<Category.Row>["columns"] = [
    {
      title: "缩略图",
      dataIndex: "picture",
      key: "picture",
      width: 100,
      align: "center",
      fixed: "left",
      render: (_, { picture }) => (
        <>{picture && <Image width={35} height={35} src={picture} />}</>
      ),
    },
    {
      title: "分类名",
      key: "name",
      dataIndex: "name",
      ellipsis: true,
      width: 180,
      align: "center",
    },
    {
      title: "创建时间",
      key: "createTime",
      dataIndex: "createTime",
      ellipsis: true,
      width: 180,
      align: "center",
    },
    {
      title: "描述",
      key: "description",
      dataIndex: "description",
      ellipsis: true,
      width: 180,
      align: "center",
    },

    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      align: "center",
      render: (_, row) => (
        <Space size={2} wrap>
          <PermissionBtn code="category.edit">
            <Button
              type="link"
              onClick={() => {
                openOptionDialog('edit',row);
              }}
            >
              编辑
            </Button>
          </PermissionBtn>
          <PermissionBtn code="category.remove">
            <Button
              type="link"
              danger
              onClick={() => {
                showConfirm(row);
              }}
            >
              删除
            </Button>
          </PermissionBtn>
        </Space>
      ),
    },
  ];
  return (
    <div className="page__wrap--g">
      <div className="form__wrap--g">
        <QueryForm
          onChange={(form: Category.Query) => {
            setPagination({
              ...pagination,
              pageNo: 1,
            });
            setSearchForm(form);
          }}
        />
        <PermissionBtn code="category.add">
          <Button
            type="primary"
            icon={<PlusOutlined />}
            size="middle"
            onClick={() => {
              openOptionDialog('add')
            }}
          >
            新增分类
          </Button>
        </PermissionBtn>
      </div>
      <div className="table__wrap--g">
        <Table
          columns={columns}
          dataSource={tableData}
          rowKey="id"
          loading={loading}
          size="small"
          sticky
          bordered
          scroll={{ x: 1300 }}
          pagination={false}
        />
      </div>
      <div className="pagination__wrap--g">
        <Pagination
          pageSize={pagination.pageSize}
          current={pagination.pageNo}
          total={pagination.total}
          showQuickJumper={true}
          showTotal={(total) => `共${total}条`}
          onChange={(page, size) => {
            setPagination({
              total: pagination.total,
              pageNo: page,
              pageSize: size,
            });
          }}
        />
      </div>
      {isModalOpen && (
        <OptionForm
          isModalOpen={isModalOpen}
          optionType={optionType}
          oldInfo={oldInfo}
          close={close}
          change={() => {
            getCategoryList();
          }}
        />
      )}
    </div>
  );
}
